package com.ids.transfer.util;

import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class XmlUtils {

	public static String getSubTagAttribute(Element root, String tagName,
			String subTagName, String attribute) {
		String returnString = "";
		NodeList list = root.getElementsByTagName(tagName);
		NodeList children;
		int innerLoop;
		for (int loop = 0; loop < list.getLength(); loop++) {
			Node node = list.item(loop);

			if (node == null)
				continue;
			children = node.getChildNodes();

			for (innerLoop = 0; innerLoop < children.getLength();) {
				Node child = children.item(innerLoop);

				if ((child != null) && (child.getNodeName() != null)
						&& (child.getNodeName().equals(subTagName))) {
					if ((child instanceof Element)) {
						return ((Element) child).getAttribute(attribute);
					}
				}
				innerLoop++;
			}

		}

		return returnString;
	}

	public static String getSubTagValue(Node node, String subTagName) {
		String returnString = "";
		NodeList children;
		int innerLoop;
		if (node != null) {
			children = node.getChildNodes();

			for (innerLoop = 0; innerLoop < children.getLength();) {
				Node child = children.item(innerLoop);

				if ((child != null) && (child.getNodeName() != null)
						&& (child.getNodeName().equals(subTagName))) {
					Node grandChild = child.getFirstChild();

					if (grandChild.getNodeValue() != null) {
						return grandChild.getNodeValue();
					}
				}
				innerLoop++;
			}

		}

		return returnString;
	}

	public static String getSubTagValue(Element root, String tagName,
			String subTagName) {
		String returnString = "";
		NodeList list = root.getElementsByTagName(tagName);
		NodeList children;
		int innerLoop;
		for (int loop = 0; loop < list.getLength(); loop++) {
			Node node = list.item(loop);

			if (node == null)
				continue;
			children = node.getChildNodes();

			for (innerLoop = 0; innerLoop < children.getLength();) {
				Node child = children.item(innerLoop);

				if ((child != null) && (child.getNodeName() != null)
						&& (child.getNodeName().equals(subTagName))) {
					Node grandChild = child.getFirstChild();

					if (grandChild.getNodeValue() != null) {
						return grandChild.getNodeValue();
					}
				}
				innerLoop++;
			}

		}

		return returnString;
	}

	public static String getTagAttribute(Element root, String tagName,
			String attribute) {
		String returnString = "";
		NodeList list = root.getElementsByTagName(tagName);

		for (int loop = 0; loop < list.getLength(); loop++) {
			Element element = (Element) list.item(loop);
			returnString = element.getAttribute(attribute);
		}

		return returnString;
	}

	public static Element getSubElement(Element root, String tagName,
			String value) {
		NodeList list = root.getElementsByTagName(tagName);

		for (int loop = 0; loop < list.getLength(); loop++) {
			Element element = (Element) list.item(loop);
			if (value.equals(element.getAttribute("value"))) {
				return element;
			}
		}

		return null;
	}

	public static String getTagValue(Element root, String tagName) {
		String returnString = "";
		NodeList list = root.getElementsByTagName(tagName);

		for (int loop = 0; loop < list.getLength(); loop++) {
			Node node = list.item(loop);

			if (node == null)
				continue;
			Node child = node.getFirstChild();

			if ((child != null) && (child.getNodeValue() != null)) {
				return child.getNodeValue();
			}

		}

		return returnString;
	}

	public static Element loadDocument(URL url) throws Exception {
		Document doc = null;
		InputSource xmlInp = new InputSource(url.openStream());
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
				.newInstance();
		DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();
		doc = parser.parse(xmlInp);

		Element root = doc.getDocumentElement();
		root.normalize();

		return root;
	}

	public static Document getDocument(URL url) throws Exception {
		Document doc = null;
		InputSource xmlInp = new InputSource(url.openStream());
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
				.newInstance();
		DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();
		doc = parser.parse(xmlInp);

		return doc;
	}
}